﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyHealthOnline.Business;
using MyHealthOnline.Dal.Generated.EntityClasses;
using MyHealthOnline.Dal.Generated.CollectionClasses;

namespace MyHealthOnline.Specialist
{
    public partial class Subscribe : System.Web.UI.Page
    {
        private int patientId
        {
            get
            {
                if (ViewState["PatientId"] == null)
                {
                    return -1;
                }
                return Convert.ToInt32( ViewState["PatientId"].ToString());
            }
            set
            {
                ViewState["PatientId"] = value;
            }
        }

        private PatientCollection PatientsDataSource
        {
            get
            {
                return ViewState["Patients"] as PatientCollection;
            }
            set
            {
                ViewState["Patients"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnCreateConsult_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (Page.IsValid)
            {
                string specialistId = ddlSpecialists.SelectedValue;
                string personalNotes=txtShortInfo.Text;
                string consultDate=txtConsultDate.Text;
                
                ConsultBusiness.CreateConsult(specialistId, personalNotes, consultDate, patientId);


            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            lblError.Visible = true;

            PatientsDataSource = PatientBusiness.GetPatients(txtPatientNo.Text, txtPatientName.Text,
                txtBirthDate.Text, rblGender.SelectedValue);
            if (PatientsDataSource.Count() > 0)
            {
                lblError.Visible = false;
            }
            else
            {
                lblError.Text = "Patient Not Found";
            }
            grdPatients.DataSource = PatientsDataSource;
            grdPatients.DataBind();
            
        }

        protected void grdPatients_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                lblError.Visible = false;
                
                int index=Convert.ToInt32(e.CommandArgument);

                PatientEntity pacient = PatientBusiness.FilterPatients(PatientsDataSource,
                    Convert.ToInt32(grdPatients.DataKeys[index].Value));

                patientId = pacient.Id;

                string specialistId = Session["id"].ToString();
                ConsultCollection consults = ConsultBusiness.GetSpecialistConsults(pacient.Consult, specialistId, patientId);
                //if the pacient's consults assigned to the current specialist are greater than 0
                if (consults.Count > 0)
                {
                    lblError.Text = "Patient has already a consult open with the specialist";
                    lblError.Visible = true;
                }
                else
                {
                    pnlSearch.Visible = false;
                    pnlSubscribe.Visible = true;

                    txtSubscribePatientName.Text = txtPatientName.Text;
                    rblSubscribeGender.SelectedIndex = rblGender.SelectedIndex;
                    txtSubscribePatientNo.Text = txtPatientNo.Text;
                    txtSubscribeBirthDate.Text  = txtBirthDate.Text;

                    ddlSpecialists.DataTextField = "Name";
                    ddlSpecialists.DataValueField = "Id";
                    ddlSpecialists.DataSource = SpecialistBusiness.GetSameDisciplineSpecialists(specialistId);
                    ddlSpecialists.DataBind();
                }
            }
        }

        protected void btnBackSearch_Click(object sender, EventArgs e)
        {
            pnlSearch.Visible = true;
            pnlSubscribe.Visible = false;
        }
    }
}